local Move = require "orders.move"
local Atk = require "orders.attack"
local lume = @@lib(lume)

local SELECT_NEXT = function ()
   local __u = Game.upool:find_selected()
   if _u then
      local _u = Game.upool:get_next_unit(__u)
      if _u then
	 _u:select()
	 _u:focus()
      end
   end
end

!(
   local MOVE = function ()
      return `
      function ()
	 Move:execute()
      end
      `
   end

   local SELECT = function ()
      return `
      function ()
	 if Game.cursor.is_hovering then return end
	 Game.state:select()
      end
      `
   end

   local R_MENU = function ()
      return `
      function ()
	 local x, y = Game.cursor:get_position()
	 local sx, sy = Game.cursor:get_scr_position()
	 Game.state:context_menu(x, y, sx, sy)
      end
      `
   end

   local TOGGLE_FULLSCREEN = function ()
      return `
      function ()
	 Game.config.fullscreen = not Game.config.fullscreen
	 _lg.setFullscreen(Game.config.fullscreen) 
      end
      `
   end

   local TOGGLE_MENU = function ()
      return `function () Game.state:menu_toggle() end`
   end

   local DRAG_SCREEN = function ()
      return ` function ()
	 local dx, dy = Game.cursor:get_delta()
	 local camera = Game.current_screen.camera
	 camera:translate(dx / camera:getScale(), dy / camera:getScale())
      end `
   end

   local ZOOM = function ()
      return ` function(x, y)
	 if not Game.cursor.is_hovering then
	    Game.current_screen:zoom_on_mouse_by (y/50)
	 end
      end
      `
   end
 )

return {
   default = {},
   deployment = {
      { "key:escape:pressed",
	--@@TOGGLE_MENU()
	love.event.quit
      },

      { "key:n:pressed", SELECT_NEXT },
      { "key:f:pressed", @@TOGGLE_FULLSCREEN() },
      
      { "mouse:1:pressed", @@SELECT() },

      { "mouse:2:pressed", @@R_MENU() },
      
      { "mouse:3:down", @@DRAG_SCREEN() },
      
      { "mouse:wheel:", @@ZOOM(x, y) }
   },
   
   play = {
      
      {"mouse:1:down", @@SELECT()},
      
      {"mouse:2:pressed",  @@MOVE()},
      
      {"mouse:3:down", @@DRAG_SCREEN()},
      
      {"key:escape:pressed",
       -- @@TOGGLE_MENU()
       love.event.quit },
      
      {"mouse:wheel:", @@ZOOM()},

      {"key:n:pressed", SELECT_NEXT},
      
   },

}
